package com.stylefeng.guns.modular.system.service.impl;

import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.persistence.model.ContestPoints;
import com.stylefeng.guns.common.persistence.model.ContestRule;
import com.stylefeng.guns.common.persistence.dao.ContestRuleMapper;
import com.stylefeng.guns.common.persistence.model.Dict;
import com.stylefeng.guns.common.persistence.service.impl.PointServiceImpl;
import com.stylefeng.guns.modular.system.service.IContestRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 竞赛调考积分 服务实现类
 * </p>
 *
 * @author jondai
 * @since 2018-01-08
 */
@Service
public class ContestRuleServiceImpl extends PointServiceImpl<ContestRuleMapper, ContestRule> implements IContestRuleService {

    @Autowired
    private IContestRuleService contestRuleService;

    public List<ContestRule> getWinRules(String companyLevelCode, String contestLevelCode) {
        Map<String,Object> condition = new HashMap<>();
        condition.put(ContestRule.contestTypeCodeDB, contestLevelCode);
        condition.put(ContestRule.companyLevelCodeDB, companyLevelCode);
        List<ContestRule> winRules = selectByMap(condition);

        List<ContestRule> winRuleList = new ArrayList<>();
        Set<String> winLevels = new HashSet<>();

        for (ContestRule contestRule : winRules) {
            if(!winLevels.contains(contestRule.getWinTypeCode())){
                winLevels.add(contestRule.getWinTypeCode());
                winRuleList.add(contestRule);
            }
        }
        return winRuleList;
    }

    public List<ContestRule> getContestRules(String companyLevelCode) {
        Map<String,Object> condition = new HashMap<>();
        condition.put(ContestRule.companyLevelCodeDB, companyLevelCode);
        List<ContestRule> contestRules = selectByMap(condition);

        List<ContestRule> contestRuleList = new ArrayList<>();
        Set<String> contestLevels = new HashSet<>();

        for (ContestRule contestRule : contestRules) {
            if(!contestLevels.contains(contestRule.getContestTypeCode())){
                contestLevels.add(contestRule.getContestTypeCode());
                contestRuleList.add(contestRule);
            }
        }
        return contestRuleList;
    }

    @Override
    public void calIntegral(ContestPoints contestPoints, Map<String, List<Dict>> dictMap) {
        //设置公司级别
        List<Dict> dicts = dictMap.get(Const.DICT_COMPANY_TYPE);
        for (Dict dict : dicts) {
            if(dict.getName().equals(contestPoints.getCompanyType())){
                contestPoints.setCompanyTypeCode(dict.getCode());
                break;
            }
        }
        //设置竞赛类型
        List<Dict> dicts1 = dictMap.get(Const.DICT_CONTEST_TYPE);
        for (Dict dict : dicts1) {
            if(dict.getName().equals(contestPoints.getContestType())){
                contestPoints.setContestTypeCode(dict.getCode());
                break;
            }
        }

        //设置获奖类型
        List<Dict> dicts2 = dictMap.get(Const.DICT_WIN_TYPE);
        for (Dict dict : dicts2) {
            if(dict.getName().equals(contestPoints.getWinType())){
                contestPoints.setWinTypeCode(dict.getCode());
            }
        }

        Double integral = contestRuleService.getIntegral(contestPoints.getCompanyTypeCode(), contestPoints.getContestTypeCode(), contestPoints.getWinTypeCode());
        contestPoints.setIntegral(integral);
    }

    @Override
    public Double getIntegral(String companyLevelCode, String contestLevelCode, String winLevelCode) {
        Map<String,Object> condition = new HashMap<>();
        condition.put(ContestRule.companyLevelCodeDB, companyLevelCode);
        condition.put(ContestRule.contestTypeCodeDB, contestLevelCode);
        condition.put(ContestRule.winTypeCodeDB, winLevelCode);
        List<ContestRule> integralRules = selectByMap(condition);

        if(integralRules != null && integralRules.size() > 0){
            return integralRules.get(0).getIntegral();
        }else {
            return 0.0;
        }
    }
}
